package com.health.auth.service;

import com.health.common.core.constant.Constants;
import com.health.common.core.constant.SecurityConstants;
import com.health.common.core.domain.R;
import com.health.common.core.exception.ServiceException;
import com.health.common.core.utils.JwtUtils;
import com.health.common.core.utils.StringUtils;
import com.health.common.core.utils.ip.IpUtils;
import com.health.common.core.utils.uuid.IdUtils;
import com.health.common.security.service.TokenService;
import com.health.personal.api.RemoteTUserService;
import com.health.system.api.RemoteUserService;
import com.health.system.api.domain.SysUser;
import com.health.system.api.model.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;

/**
 * @BelongsProject: dimensionality-manage
 * @BelongsPackage: com.health.auth.service
 * @Author: 许力匀
 * @CreateTime: 2024-12-13  22:26
 * @Description: TODO
 * @Version: 1.0
 */
@Component
public class PatientLoginService {

    @Autowired
    private RemoteUserService remoteUserService;


    @Autowired
    private RemoteTUserService remoteTUserService ;

    @Autowired
    private TokenService tokenService ;



    public Map<String, Object> loginEmail(String email, String password)
    {
        //非空校验
        if (StringUtils.isEmpty(email)){
            throw new ServiceException("请输入邮件不能为空");
        }
        if (StringUtils.isEmpty(password)){
            throw new ServiceException("请输入密码不能为空");
        }
        //正则校验
        if (!email.matches("^[a-zA-Z0-9_+&*-]+(?:\\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\\.)+[a-zA-Z]{2,7}$")) {
            throw new ServiceException("请输入正确的邮件格式");
        }
        // 查询用户信息
        R<LoginUser> tUserPoR = remoteTUserService.selectByEmail(email);
        //非空校验
        if(tUserPoR.getData().getTuserPo() == null){
            throw  new ServiceException("您还没有注册请先注册");
        }
        //校验密码
       if(!password.equals(tUserPoR.getData().getTuserPo().getPassword())){
          throw  new ServiceException("请输入正确的密码");
       }
       return   creatToken(tUserPoR.getData());


    }


    public Map<String, Object> creatToken(LoginUser loginUser)
    {
        String token = IdUtils.fastUUID();
        loginUser.setToken(token);
        loginUser.setUserid(Long.valueOf(loginUser.getTuserPo().getId()));
        loginUser.setUsername(loginUser.getTuserPo().getRealName());
        loginUser.setIpaddr(IpUtils.getIpAddr());
        tokenService.refreshToken(loginUser);

        Map<String, Object> claimsMap = new HashMap<String, Object>();
        claimsMap.put(SecurityConstants.USER_KEY, token);
        claimsMap.put(SecurityConstants.DETAILS_USER_ID, loginUser.getTuserPo().getId());
        claimsMap.put(SecurityConstants.DETAILS_USERNAME, loginUser.getTuserPo().getRealName());
        // 接口返回信息
        Map<String, Object> rspMap = new HashMap<String, Object>();
        rspMap.put("access_token", JwtUtils.createToken(claimsMap));
        rspMap.put("expires_in", 720);
        return  rspMap;
    }


}
